package cn.cvs.controller;

import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.sysRole.SysRoleService;
import cn.cvs.utils.Constants;
import com.alibaba.fastjson.JSON;
import com.mysql.cj.util.StringUtils;
import org.aspectj.apache.bcel.classfile.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 许欣
 * 时间：2022/4/30 9:21
 * 描述：
 */
@Controller
@RequestMapping(value = "/role")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    //获取角色列表
    @RequestMapping(value = "/list")
    public String list(Model model) throws Exception {
        List<SysRole> roleList=null;
        roleList=sysRoleService.getRoleList();
        model.addAttribute("roleList",roleList);
        return "/sysRole/list";
    }
    //添加
    @RequestMapping(value = "/toAddRole")
    public String toAddRole(){
        return "/sysRole/add";
    }
    @GetMapping(value = "/addRole")
    public String addRole(@Valid SysRole role, BindingResult result) throws Exception {
        sysRoleService.add(role);
        return "redirect:/role/list";
    }
    @GetMapping(value = "/add")
    public String add(SysRole sysRole) throws Exception {
        sysRoleService.add(sysRole);
        return "redirect:/role/list";
    }
    //根据id获取
    @RequestMapping(value = "/byId")
    public String byId(@PathVariable Integer id,Model model) throws Exception {
       SysRole sysRole= sysRoleService.getById(id);
        model.addAttribute("sysRole",sysRole);
        return "redirect:/role/list";
    }
    //删除
    @RequestMapping(value = "/del/{id}")
    public String del(@PathVariable Integer id) throws Exception {
        sysRoleService.deleteById(id);
        return "redirect:/role/list";
    }
    //修改
    @RequestMapping(value = "/toUpdate")
    public String toUpdate(@RequestParam Integer id,Model model) throws Exception {
        SysRole sysUser=sysRoleService.getById(id);
        model.addAttribute(sysUser);
        return "sysRole/update";
    }
    @RequestMapping(value = "/update")
    public String update(SysRole sysRole) throws Exception {
        sysRoleService.update(sysRole);
        return "redirect:/role/list";
    }
    @ResponseBody
    @GetMapping("/userExist")
    public Object userExist(@RequestParam String code) throws Exception {

        HashMap<String,Object>map=new HashMap<>();
        if (StringUtils.isNullOrEmpty(code)){
            map.put("exist",1);
        }else {
            SysRole sysRole=sysRoleService.getByCode(code);
            if (null!=sysRole){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }
        return JSON.toJSONString(map);
    }
}
